//
//  PEMChallengeModel.swift
//  PhaseExcitedMotion
//
//  Created by iOS-Czz on 2023/11/10.
//

import Foundation
class PEMChallengeViewModel{
    var dataSource = [PEMChallengeModel]()
    var completed = 0
    var all = 0
   let viewModel = PEMDailyExerciseViewModel()
}
extension PEMChallengeViewModel{
    func loadData(_ date:String, onSuccess: ((_ models: [PEMChallengeModel]) ->Void)?){
        let dataHelper = PEMChallengeModel.getUsingLKDBHelper()
        let sql = "select * from @t where date = '\(date)'"
        let modelArr = dataHelper.search(withSQL: sql, to: PEMChallengeModel.self) as! [PEMChallengeModel]
//        let dateFormatter = DateFormatter()
//        dateFormatter.dateFormat = "yyyy.MM.dd"
//        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
//            guard let date1 = dateFormatter.date(from: model1.date),
//                  let date2 = dateFormatter.date(from: model2.date) else {
//                return false // 如果转换失败则直接返回 false
//            }
//            return date1 > date2
//        }
        viewModel.loadAllData(date) {[self] models in
            for model in modelArr {
                /// 0 duration 1 distance 2 step 3 energy
                if model.type == 0 {
                    if !(viewModel.duration < model.target) {
                        model.completed = true
                    }else{
                        model.completed = false
                    }
                    model.progress = viewModel.duration
                }else if model.type == 1 {
                    if !(viewModel.distanceAll < model.target) {
                        model.completed = true
                    }else{
                        model.completed = false
                    }
                    model.progress = viewModel.distanceAll * 1000
                }else if model.type == 2 {
                    if !(viewModel.step < model.target) {
                        model.completed = true
                    }else{
                        model.completed = false
                    }
                    model.progress = viewModel.step
                }else if model.type == 3 {
                    if !(viewModel.engery < model.target) {
                        model.completed = true
                    }else{
                        model.completed = false
                    }
                    model.progress = viewModel.engery
                }
            }
        }
        self.all = modelArr.count
        self.completed = modelArr.filter { $0.completed }.count
        self.dataSource = modelArr
        onSuccess?(self.dataSource)
    }
    
    
}

@objcMembers
class PEMChallengeModel: PEMModel,Codable {
    var date = ""
    var name = ""
    var target = 0.0
    var progress = 0.0
    /// 0 duration 1 distance 2 step 3 energy
    var type = 0
    var rewards = ""
    var completed = false
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: PEMChallengeModel.self)
    }
}
